using UnityEngine;
using Zero;
using Random = UnityEngine.Random;

[DefaultExecutionOrder(-10000)]
public class WorldNumberService : MonoBehaviour, IWorldNumberService
{
    [SerializeField] private float _baseHeight;
    [SerializeField] private float _randomDistance;
    [SerializeField] private WorldNumber _damageNumber;
    [SerializeField] private WorldNumber _criticalDamageNumber;
    [SerializeField] private WorldNumber _healNumber;
    [SerializeField] private WorldNumber _criticalHealNumber;
    [SerializeField] private WorldNumber _missText;
    [SerializeField] private WorldNumber _invulnerableText;
    [SerializeField] private WorldNumber _elementReactionText;

    private void OnEnable()
    {
        ServiceLocator.Register<IWorldNumberService>(this);
    }

    private void OnDisable()
    {
        ServiceLocator.Unregister<IWorldNumberService>();
    }

    public void CreateDamage(DamageNumberInfo damageNumberInfo)
    {
        CreateNumber(damageNumberInfo.IsCritical ? _criticalDamageNumber : _damageNumber,
            damageNumberInfo.Position, damageNumberInfo.Amount, damageNumberInfo.ElementType);
    }

    public void CreateHeal(HealNumberInfo healNumberInfo)
    {
        CreateNumber(healNumberInfo.IsCritical ? _criticalHealNumber : _healNumber,
            healNumberInfo.Position, healNumberInfo.Amount, ElementType.None);
    }

    public void CreateMiss(Vector3 position)
    {
        CreateText(_missText, position);
    }

    public void CreateInvulnerable(Vector3 position)
    {
        CreateText(_invulnerableText, position);
    }

    public void CreateElementReaction(Vector3 position, string reaction)
    {
        var randomOffset = Random.insideUnitSphere * _randomDistance;
        randomOffset.y = 0f;
        var worldNumber = Instantiate(_elementReactionText, position + randomOffset, Quaternion.identity);

        if (reaction == "燃烧")
        {
            worldNumber.Text = "Burning";
            worldNumber.Color = ServiceLocator.Get<IElementService>().GetColor(ElementType.Fire);
        }
        else if (reaction == "感电")
        {
            worldNumber.Text = "Elctro-Charged";
            worldNumber.Color = ServiceLocator.Get<IElementService>().GetColor(ElementType.Electric);
        }
        else if (reaction == "蒸发")
        {
            worldNumber.Text = "Vaporize";
            worldNumber.Color = ServiceLocator.Get<IElementService>().GetColor(ElementType.Fire);
        }
        else if (reaction == "融化")
        {
            worldNumber.Text = "Melt";
            worldNumber.Color = ServiceLocator.Get<IElementService>().GetColor(ElementType.Fire);
        }
        else if (reaction == "冻结")
        {
            worldNumber.Text = "Frozen";
            worldNumber.Color = ServiceLocator.Get<IElementService>().GetColor(ElementType.Ice);
        }
        else if (reaction == "扩散")
        {
            worldNumber.Text = "Swirl";
            worldNumber.Color = ServiceLocator.Get<IElementService>().GetColor(ElementType.Wind);
        }
        else if (reaction == "原激化")
        {
            worldNumber.Text = "Quicken";
            worldNumber.Color = ServiceLocator.Get<IElementService>().GetColor(ElementType.Grass);
        }
        else if (reaction == "超激化")
        {
            worldNumber.Text = "Aggravate";
            worldNumber.Color = ServiceLocator.Get<IElementService>().GetColor(ElementType.Electric);
        }
        else if (reaction == "蔓激化")
        {
            worldNumber.Text = "Spread";
            worldNumber.Color = ServiceLocator.Get<IElementService>().GetColor(ElementType.Grass);
        }
        else if (reaction == "超载")
        {
            worldNumber.Text = "Overloaded";
            worldNumber.Color = ServiceLocator.Get<IElementService>().GetColor(ElementType.Fire);
        }
        else if (reaction == "超导")
        {
            worldNumber.Text = "Superconduct";
            worldNumber.Color = ServiceLocator.Get<IElementService>().GetColor(ElementType.Electric);
        }
        else if (reaction == "绽放")
        {
            worldNumber.Text = "Bloom";
            worldNumber.Color = ServiceLocator.Get<IElementService>().GetColor(ElementType.Grass);
        }
        else if (reaction == "结晶")
        {
            worldNumber.Text = "Crystallize";
            worldNumber.Color = ServiceLocator.Get<IElementService>().GetColor(ElementType.Rock);
        }

        worldNumber.Play();
    }

    private void CreateNumber(WorldNumber prefab, Vector3 position, float damage, ElementType elementType)
    {
        var randomOffset = Random.insideUnitSphere * _randomDistance;
        randomOffset.y = Mathf.Abs(randomOffset.y) + _baseHeight;
        var worldNumber = Instantiate(prefab,
            position + randomOffset,
            Quaternion.identity);
        worldNumber.Text = ((int)damage).ToString();
        worldNumber.TextMesh.color = ServiceLocator.Get<IElementService>().GetColor(elementType);
        worldNumber.Play();
    }

    private void CreateText(WorldNumber prefab, Vector3 position)
    {
        var randomOffset = Random.insideUnitSphere * _randomDistance;
        randomOffset.y = Mathf.Abs(randomOffset.y) + _baseHeight;
        var worldNumber = Instantiate(prefab,
            position + randomOffset,
            Quaternion.identity);
        worldNumber.Play();
    }
}